package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.service.DishService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("dish")
public class DishController {

    @Autowired
    private DishService dishService;

    /**
     * 新增菜品
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto) {
        dishService.saveWithFlavors(dishDto);
        return R.success("添加菜品成功");
    }

    /**
     * 分页查询（多表查询）
     */
    @GetMapping("page")
    public R<Page<DishDto>> pageDishDto(Integer page, Integer pageSize, String name) {
        Page<DishDto> pageInfo = dishService.pageDishDto(page, pageSize, name);
        return R.success(pageInfo);
    }

    /**
     * 根据id查询菜品详情数据，包括口味集合数据
     */
    @GetMapping("{id}")
    public R<DishDto> findById(@PathVariable Long id) {
        DishDto dishDto = dishService.getByIdWithFlavors(id);
        return R.success(dishDto);
    }

    /**
     * 更新菜品数据，以及口味信息
     */
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto) {
        dishService.updateWithFlavor(dishDto);
        return R.success("修改成功");
    }


    /**
     * 批量停售、启售
     */
    @PostMapping("status/{status}")
    public R<String> updateStatus(@PathVariable Integer status, Long[] ids) {
        //update dish set status=? where id in ( ids )
        dishService.update()
                .set("status", status)
                .in("id", ids)
                .update();
        return R.success("操作成功");
    }

    /**
     * 根据分类查询菜品列表
     */
    @GetMapping("list")
    public R<List<DishDto>> list(Long categoryId, String name) {
        List<DishDto> list = dishService.listDishDto(categoryId, name);
        return R.success(list);
    }
}
